package com.education.platform.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.platform.common.Result;
import com.education.platform.dto.article.ArticleCreateDTO;
import com.education.platform.entity.Article;
import com.education.platform.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章控制器
 *
 * @author education-platform
 * @since 2024-09-27
 */
@Tag(name = "文章管理", description = "文章管理相关接口")
@RestController
@RequestMapping("/api/article")
@RequiredArgsConstructor
public class ArticleController {

    private final ArticleService articleService;

    @Operation(summary = "创建文章", description = "创建新文章")
    @PostMapping
    @SaCheckLogin
    public Result<Long> createArticle(@Validated @RequestBody ArticleCreateDTO articleCreateDTO) {
        Long articleId = articleService.createArticle(articleCreateDTO);
        return Result.success(articleId);
    }

    @Operation(summary = "更新文章", description = "更新文章信息")
    @PutMapping("/{id}")
    @SaCheckLogin
    public Result<Void> updateArticle(@PathVariable Long id, 
                                     @Validated @RequestBody ArticleCreateDTO articleCreateDTO) {
        articleService.updateArticle(id, articleCreateDTO);
        return Result.success();
    }

    @Operation(summary = "删除文章", description = "删除文章")
    @DeleteMapping("/{id}")
    @SaCheckLogin
    public Result<Void> deleteArticle(@PathVariable Long id) {
        articleService.deleteArticle(id);
        return Result.success();
    }

    @Operation(summary = "获取文章详情", description = "根据ID获取文章详细信息")
    @GetMapping("/{id}")
    public Result<Article> getArticleDetail(@PathVariable Long id) {
        Article article = articleService.getArticleById(id);
        // 增加浏览次数
        articleService.incrementViewCount(id);
        return Result.success(article);
    }

    @Operation(summary = "分页查询文章列表", description = "分页查询文章列表")
    @GetMapping("/page")
    public Result<IPage<Article>> getArticlePage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String tags) {
        Page<Article> page = new Page<>(current, size);
        IPage<Article> result = articleService.getArticlePage(page, status, keyword, tags);
        return Result.success(result);
    }

    @Operation(summary = "获取我的文章列表", description = "获取当前用户的文章列表")
    @GetMapping("/my")
    @SaCheckLogin
    public Result<List<Article>> getMyArticles() {
        Long userId = StpUtil.getLoginIdAsLong();
        List<Article> articles = articleService.getMyArticles(userId);
        return Result.success(articles);
    }

    @Operation(summary = "发布文章", description = "发布文章")
    @PostMapping("/{id}/publish")
    @SaCheckLogin
    public Result<Void> publishArticle(@PathVariable Long id) {
        articleService.publishArticle(id);
        return Result.success();
    }

    @Operation(summary = "归档文章", description = "归档文章")
    @PostMapping("/{id}/archive")
    @SaCheckLogin
    public Result<Void> archiveArticle(@PathVariable Long id) {
        articleService.archiveArticle(id);
        return Result.success();
    }

    @Operation(summary = "点赞文章", description = "点赞文章")
    @PostMapping("/{id}/like")
    public Result<Void> likeArticle(@PathVariable Long id) {
        articleService.incrementLikeCount(id);
        return Result.success();
    }

    @Operation(summary = "获取热门文章", description = "获取热门文章列表")
    @GetMapping("/hot")
    public Result<List<Article>> getHotArticles(@RequestParam(defaultValue = "10") Integer limit) {
        List<Article> articles = articleService.getHotArticles(limit);
        return Result.success(articles);
    }

    @Operation(summary = "根据标签查询文章", description = "根据标签查询文章列表")
    @GetMapping("/tag/{tag}")
    public Result<List<Article>> getArticlesByTag(@PathVariable String tag) {
        List<Article> articles = articleService.getArticlesByTag(tag);
        return Result.success(articles);
    }
}

